/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : friend_group.js                            
*  Description  : 친구그룹핑
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-24  김효순    Created.  
*  2012-04-24  김효순    액션빼고 완료.  
*******************************************************************************/

jQuery(function($){
	// 그룹리스트
	function clsGroupList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.groupIDX = null;
		
		this._init();
	}
	
	clsGroupList.prototype = {
		_init: function(){
			$layer = $("div#container>section#main_content>article.my_group_left>figure")
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			$layer.find("tbody").empty();
			
			// 전체부 설정
			this.$getBtnAll().css("cursor", "pointer");
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $box = $layer.find("tbody");
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$box.empty();
			for(var i=0;i<this.listData.length;i++){
				$box.append($element.clone(true));
			}
			
			$box.find("tr").each(function(n){
				$(this).find("td:eq(0)").html(thisClass.listData[n]["그룹명"] + "<span>(" + thisClass.listData[n]["친구수"] + ")</span>");
				if(n == thisClass.groupIDX){
					$(this).find("td:eq(1) a").hide();
				}else{
					$(this).find("td:eq(1) a").show();
				}
				
				// 친구추가 버튼
				$(this).find("td:eq(1)>a").click(function(event){
					event.preventDefault();
					
					var $thisTR = $(this).parents("tr");
					thisClass.setIDX($box.find("tr").index($thisTR));
				});
			});
		}
		, setIDX: function(idx){
			this.groupIDX = idx;
			this.display();
			
			$(this).trigger("evtGroupClick");
		}
		, getGroupIDX: function(){
			return this.groupIDX;
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("table").hide();;
			$layer.append("<div id='tmpSpin' style='position:absolute; width:400px; height:100px;'></div>").find("#tmpSpin").spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("#tmpSpin").spin(false).remove();
			$layer.find("table").show();
		}
		, $getBtnAll: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.find("figcaption");
		}
	}
	
	// 친구목록 내 그룹
	function clsGroupLayer()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.groupIDX = null;
		
		this._init();
	}
	
	clsGroupLayer.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content aside.my_group_right dl.friend_cont dt:first");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("div.all_list li:first").clone(true)
			}
			
			$layer.find("div.all_list").hide().find("ul").empty();
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $box = $layer.find("ul");
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$box.empty();
			for(var i=0;i<this.listData.length;i++){
				$box.append($element.clone(true));
			}
			
			$box.find("li").each(function(n){
				$(this).find("a").html(thisClass.listData[n]["그룹명"] + "(" + thisClass.listData[n]["친구수"] + ")").click(function(event){
					event.preventDefault();
					
					thisClass.setIDX(n);
				});
			});
			$layer.find("p.tit").click(function(event){
				event.preventDefault();
				
				thisClass.setIDX(null);
			});
		}
		, displayTitle: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if(this.groupIDX == null) var szTitle = "전체";
			else var szTitle = this.listData[this.groupIDX]["그룹명"];
			
			$layer.find("span.bt_all").html(szTitle);
		}
		// 펼침 버튼
		, $getBtnSelect: function(){
			return this._HTMLDom["$layer"].find("span.bt_all");
		}
		, show: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div.all_list").show(300);
		}
		, hide: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div.all_list").hide();
		}
		, setIDX: function(idx){
			this.groupIDX = idx;
			this.displayTitle();
			this.hide();
			
			$(this).trigger("evtSelect");
		}
		, getGroupKey: function(){
			if(this.groupIDX == null) return null;
			else return this.listData[this.groupIDX]["키"];
		}
	}
	
	// 친구
	function clsFriendList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsFriendList.prototype = {
		_init: function(){
			$layer = $("div#container>section#main_content>aside.my_group_right dd.friend_list")
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			$layer.find("tbody").empty();
		}
		, setData: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $box = $layer.find("tbody");
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$box.empty();
			for(var i=0;i<this.listData.length;i++){
				$box.append($element.clone(true));
			}
			
			$box.find("tr").each(function(n){
				switch(thisClass.listData[n]["성별"]){
					case "남자":
						var szClass = "sex_m";
						break;
					case "여자":
						var szClass = "sex_w";
						break;
				}
				
				$(this).find("td:eq(1) img").attr("src", thisClass.listData[n]["이미지"]);
				$(this).find("td:eq(2) a").html(thisClass.listData[n]["닉네임"]);
				$(this).find("td:eq(3) li:eq(0)").attr("class", szClass).html(thisClass.listData[n]["성별"]);
				$(this).find("td:eq(3) li:eq(1) span").html(thisClass.listData[n]["핸디"]);
				$(this).find("td:eq(3) li:eq(2) span").html(thisClass.listData[n]["비거리"]);
			});
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("table").hide();;
			$layer.append("<div id='tmpSpin' style='position:absolute; width:400px; height:100px;'></div>").find("#tmpSpin").spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("#tmpSpin").spin(false).remove();
			$layer.find("table").show();
		}
	}
	
	// 서버통신
	function clsComm()
	{
		this.groupList = null;
		this.friendList = null;
		this.totalFriend = null;
		
		this.friendRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.groupList = [
//				{
//					"키": null
//					, "그룹명": null
//					, "친구수": null
//				}
			];
			
			this.friendList = [
//				{
//					"아이디": null
//					, "이미지": null
//					, "닉네임": null
//					, "그룹키": null
//					, "성별": null
//					, "핸디": null
//					, "비거리": null
//				}
			];
			
			this.friendRequest = {
				"그룹키": null
			}
		}
		, getTotalFriend: function(){
			var thisClass = this;
			//ajax
			setTimeout(function(){
				thisClass.totalFriend = 102;
				
				$(thisClass).trigger("evtFinishTotalFriend");
			}, 500);
		}
		, getTotalFriendData: function(){
			return this.totalFriend;
		}
		, getGroupList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.groupList = [
					{
						"키": "001"
						, "그룹명": "중학교동창회"
						, "친구수": 3
					}
					, {
						"키": "002"
						, "그룹명": "비즈니스모임"
						, "친구수": 13
					}
					, {
						"키": "003"
						, "그룹명": "학교"
						, "친구수": 22
					}
				];
				
				$(thisClass).trigger("evtFinishGroupList");
			}, 1500);
		}
		, getGroupData: function(){
			return this.groupList;
		}
		, getFriendList: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				thisClass.friendList = [
					{
						"아이디": "sky72"
						, "이미지": "../images/board/default_35_35.gif"
						, "닉네임": "잭니클라우스"
						, "그룹키": "001"
						, "성별": "남자"
						, "핸디": "100"
						, "비거리": "200"
					}
					, {
						"아이디": "sky73"
						, "이미지": "../images/board/default_35_35.gif"
						, "닉네임": "잭니클라우스1"
						, "그룹키": "001"
						, "성별": "여자"
						, "핸디": "200"
						, "비거리": "300"
					}
					, {
						"아이디": "sky74"
						, "이미지": "../images/board/default_35_35.gif"
						, "닉네임": "잭니클라우스2"
						, "그룹키": "001"
						, "성별": "남자"
						, "핸디": "300"
						, "비거리": "400"
					}
				];
				
				$(thisClass).trigger("evtFinishFriendList");
			}, 1500);
		}
		, getFriendData: function(){
			return this.friendList;
		}
		, setFriendRequest: function(key, val){
			this.friendRequest[key] = val;
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objGroupList = null;
		this.objFriendList = null;
		this.objGroupLayer = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objGroupList = new clsGroupList();
			this.objFriendList = new clsFriendList();
			this.objGroupLayer = new clsGroupLayer();
			
			this._bind();
			
			this.objGroupList.spinStart();
			this.objComm.getGroupList();
			
			this.objFriendList.spinStart();
			this.objComm.getFriendList();
		}
		, _bind: function(){
			var thisClass = this;
			
			$(this.objComm).bind("evtFinishGroupList", function(){
				var thisData = this.getGroupData();
				
				thisClass.objGroupList.setData(thisData);
				thisClass.objGroupList.spinEnd();
				thisClass.objGroupLayer.setData(thisData);
			});
			
			$(this.objComm).bind("evtFinishFriendList", function(){
				var thisData = this.getFriendData();
				
				thisClass.objFriendList.setData(thisData);
				thisClass.objFriendList.spinEnd();
			});
			
			// 그룹리스트 내 펼침 버튼
			this.objGroupLayer.$getBtnSelect().click(function(event){
				event.preventDefault();
				
				thisClass.objGroupLayer.show();
			});
			
			// 그룹리스트 내 펼침 선택
			$(this.objGroupLayer).bind("evtSelect", function(){
				// 친구목록 갱신
				var groupKey = this.getGroupKey();
				
				thisClass.objFriendList.spinStart();
				thisClass.objComm.setFriendRequest("그룹키", groupKey);
				thisClass.objComm.getFriendList();
			});
			
			// 그룹리스트 내 친구추가
			$(this.objGroupList).bind("evtGroupClick", function(){
				// 친구목록 내 셀렉트 변경
				var idx = this.getGroupIDX();
				
				thisClass.objGroupLayer.setIDX(idx);
			});
			
			// 그룹리스트 내 전체버튼
			this.objGroupList.$getBtnAll().click(function(event){
				thisClass.objGroupList.setIDX(null);
			});
		}
	}
	
	document.objControl = new clsControl();
});
